﻿using System;
using System.Reflection;
using System.Linq;
using SAO.OracleHelper.ValiDateModel;

namespace SAO.OracleHelper
{
    public static class SqlCache<T>
    {
        public static string FindSql = null;
        public static string QuerySql = null;
        public static string InsertSql = null;
        public static string DeleteSql = null;
        public static string UpdateSql = null;

        static SqlCache()
        {
            Type type = typeof(T);
            PropertyInfo[] propertyInfos = type.GetProperties();
            string key = type.GetKey();
            FindSql = $"SELECT {string.Join(",", propertyInfos.Select(s => $"{s.GetName()}"))} FROM {type.GetName()} WHERE {key} = :{key}";
            QuerySql = $"SELECT {string.Join(",", propertyInfos.Select(s => $"{s.GetName()}"))} FROM {type.GetName()}";
            InsertSql = $"INSERT INTO {type.GetName()}({string.Join(",", propertyInfos.Where(w => !w.GetName().Equals(key)).Select(s => $"{s.GetName()}"))}) VALUES({string.Join(",", propertyInfos.Select(s => $":{s.GetName()}"))})";
            DeleteSql = $"DELETE FROM {type.GetName()} WHERE {key} = :{key}";
            UpdateSql = $"UPDATE {type.GetName()} SET {string.Join(",", propertyInfos.Where(w => !w.GetName().Equals(key)).Select(s => $"{s.GetName()}=:{s.GetName()}"))} WHERE {key} = :{key}";
        }
    }
}
